﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace WickedSick.ClientComponents.Music
{
    [Serializable]
    public class Playlist : ISerializable
    {
        private int _ID;
        private string _Name;
        private string _Description;
        private List<Song> _Songs;

        public Playlist()
        {
            this.Songs = new List<Song>();
        }

        public Playlist(SerializationInfo info, StreamingContext context)
        {
            this.ID = info.GetInt32("ID");
            this.Name = info.GetString("Name");
            this.Description = info.GetString("Description");
            this.Songs = (List<Song>)info.GetValue("Songs", typeof(List<Song>));
        }

        public Playlist(int id, string new_playlist)
        {
            string delim = "<|>";
            //[Name]<|>[Description]<|>[Song 1 ID],[Song 2 ID],[Song 3 ID]
            string[] tokens = new_playlist.Split(new string[] { delim }, StringSplitOptions.None);

            if (tokens.Length != 3)
                throw new Exception("Invalid playlist protocol");

            this.ID = id;
            this.Name = tokens[0];
            this.Description = tokens[1];

            this.Songs = new List<Song>();
            string[] songs = tokens[2].Split(',');
            foreach (string s in songs)
            {
                int temp = 0;
                int.TryParse(s, out temp);
                Song new_song = new Song();
                new_song.ID = temp;
                this.Songs.Add(new_song);
            }
        }

        public void ModifyField(string field_name, string new_value)
        {
            if (field_name == "Name")
            {
                Name = new_value;
            }
            else if (field_name == "Description")
            {
                Description = new_value;
            }
        }

        public int ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public string Description
        {
            get { return _Description; }
            set { _Description = value; }
        }

        public List<Song> Songs
        {
            get { return _Songs; }
            set { _Songs = value; }
        }

        #region ISerializable Members

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("ID", ID);
            info.AddValue("Name", Name);
            info.AddValue("Description", Description);
            info.AddValue("Songs", Songs);
        }

        #endregion
    }
}
